/**
 * 
 */
package com.nari.basic.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.nari.basic.model.IRole;
import com.nari.coreframe.dao.hibernate3.GenericDaoImpl;

/**
 * 
 * @author Xiaopeng
 * @date 2013-7-18
 */
public class RoleDaoImpl extends GenericDaoImpl<IRole, String> implements IRoleDao {

	/**
	 * 通过用户唯一识别码查询其对应的角色信息
	 * @param userUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-7-22
	 */
	public List<IRole> queryRolesByUserUID(String userUID) throws Exception {
		if(userUID!=null && !"".equals(userUID)) {
			String queryUserRoleHql = "from IRole r " +
					"where r.id in(select ur.roleUuid " +
								"from IUserRole ur " +
								"where ur.userUuid='"+userUID+"') " +
					"order by r.orderID";
			return this.runQueryHSql(queryUserRoleHql);
		}
		return null;
	}
	
	/**
	 * 通过用户登录名查询其授权的角色编号
	 * @param userID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-13
	 */
	public String queryRoleIDsByUserID(String userID) throws Exception {
		StringBuffer roleIDs = new StringBuffer("");
		if(userID!=null && !"".equals(userID)) {
			String hsql = "select r.roleID from IRole r " +
					"where r.id in(select ur.roleUuid " +
								"from IUserRole ur " +
								"where ur.userUuid=(select u.id from IUser u " +
												"where u.userID='"+userID+"')) " +
					"order by r.orderID";
			List<Object> roleIDList = this.runQueryHSqlObject(hsql);
			if(roleIDList!=null && roleIDList.size()>0) {
				for(int index=0; index<roleIDList.size(); index++) {
					roleIDs.append(roleIDList.get(index));
					if(index < roleIDList.size()-1) {
						roleIDs.append(",");
					}
				}
			}
		}
		return roleIDs.toString();
	}
	
	/**
	 * 查询系统所有角色信息，并对角色列表排序
	 * @param roleID 角色编号（可选）
	 * @param roleName 角色名称（可选）
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-7-26
	 */
	@SuppressWarnings("unchecked")
	public List<IRole> queryRoleInOrder(String roleID, String roleName) throws Exception {
		Criteria criteria = this.getCurrentSession().createCriteria(IRole.class);
		if(roleID!=null && !"".equals(roleID.trim())) {
			criteria.add(Restrictions.ilike("roleID", roleID, MatchMode.ANYWHERE));
		}
		if(roleName!=null && !"".equals(roleName.trim())) {
			criteria.add(Restrictions.ilike("roleName", roleName, MatchMode.ANYWHERE));
		}
		criteria.addOrder(Order.asc("orderID")).addOrder(Order.asc("roleID"));
		return criteria.list();
	}
	
	/**
	 * 根据角色编号查询角色信息
	 * @param roleID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-1-12
	 */
	public IRole queryRoleInfoByRoleID(String roleID) throws Exception {
		String hsql = "from IRole r where r.roleID='"+roleID+"'";
		List<IRole> roleList = this.runQueryHSql(hsql);
		//角色信息
		IRole role = null;
		if(roleList!=null && roleList.size()>0) {
			role = roleList.get(0);
		}
		return role;
	}
	
	/**
	 * 统计相同角色编号的角色个数(排除指定的角色)
	 * @param roleID
	 * @param roleUuid
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-7-21
	 */
	public int countRolesByRoleID(String roleID, String roleUuid) throws Exception {
		String hsql = "select count(*) from IRole r " +
				"where r.roleID='"+roleID+"'";
		if(roleUuid!=null && !"".equals(roleUuid)) {
			hsql += " and r.id<>'"+roleUuid+"'";
		}
		return ((Long)this.getCurrentSession().createQuery(hsql).uniqueResult()).intValue();
	}
}
